package com.jrzh.mvc.service.sys.impl;

import java.io.File;
import java.io.Serializable;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jrzh.common.exception.ProjectException;
import com.jrzh.common.tools.CollectionTools;
import com.jrzh.config.ProjectConfigration;
import com.jrzh.framework.base.convert.BaseConvertI;
import com.jrzh.framework.base.repository.BaseRepository;
import com.jrzh.framework.base.service.impl.BaseServiceImpl;
import com.jrzh.framework.bean.SessionUser;
import com.jrzh.mvc.constants.ConfigConstants;
import com.jrzh.mvc.convert.sys.EmailSendConvert;
import com.jrzh.mvc.model.sys.EmailSendModel;
import com.jrzh.mvc.model.sys.EmailSendingModel;
import com.jrzh.mvc.model.sys.FileModel;
import com.jrzh.mvc.repository.sys.EmailSendRepository;
import com.jrzh.mvc.search.sys.EmailSendSearch;
import com.jrzh.mvc.service.sys.EmailSendServiceI;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.EmailSendView;
import com.jrzh.notify.email.EmailBean;
import com.jrzh.notify.email.SendEmail;

@Service("emailSendService")
public class EmailSendServiceImpl extends
		BaseServiceImpl<EmailSendModel, EmailSendSearch, EmailSendView>
		implements EmailSendServiceI {

	@Autowired
	private EmailSendRepository emailSendRepository;

	@Autowired
	private SysServiceManage sysServiceManage;

	@Autowired
	private ProjectConfigration projectConfigration;

	@Override
	public BaseRepository<EmailSendModel, Serializable> getDao() {
		return emailSendRepository;
	}

	@Override
	public BaseConvertI<EmailSendModel, EmailSendView> getConvert() {
		return new EmailSendConvert();
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@Override
	public void saveSend(EmailSendingModel sending) throws ProjectException,
			GeneralSecurityException {
		EmailSendModel send = new EmailSendModel();
		send.setBody(sending.getBody());
		send.setEmail(sending.getEmail());
		send.setFileIds(sending.getFileIds());
		send.setIsDisable(false);
		send.setIsLock(false);
		send.setTitle(sending.getTitle());
		send.setSortNum(1);
		send.setTryTimes(sending.getTryTimes());
		sysServiceManage.emailSendService.add(send, SessionUser.getSystemUser());
		sysServiceManage.emailSendingService.delete(sending.getId(), SessionUser.getSystemUser());
		EmailBean mailInfo = new EmailBean();
		mailInfo.setMailServerHost(sysServiceManage.configService
				.getValue(ConfigConstants.EMAIL_SMTP));
		mailInfo.setMailServerPort(sysServiceManage.configService
				.getValue(ConfigConstants.EMAIL_PORT));
		mailInfo.setFromAddress(sysServiceManage.configService
				.getValue(ConfigConstants.EMAIL_ACCOUNT));
		mailInfo.setPassword(sysServiceManage.configService
				.getValue(ConfigConstants.EMAIL_PASSWORD));
		mailInfo.setValidate(true);
		mailInfo.setContent(sending.getBody());
		mailInfo.setSubject(sending.getTitle());
		mailInfo.setToAddress(sending.getEmail());
		mailInfo.setUserName(sysServiceManage.configService
				.getValue(ConfigConstants.EMAIL_ACCOUNT));
		String fileIds = sending.getFileIds();
		Map<String, File> atts = new HashMap<String, File>();
		if (StringUtils.isNotBlank(fileIds) && fileIds.length() > 0) {
			String[] _fileIds = fileIds.split(",");
			if (null != _fileIds && _fileIds.length > 0) {
				List<FileModel> fileList = sysServiceManage.fileService
						.listInByField("id", _fileIds);
				if (CollectionTools.isNotBlank(fileList)) {
					for (FileModel file : fileList) {
						String url = file.getUrl().replace(
								projectConfigration.mappingUrl,
								projectConfigration.getFileRootUrl());
						File _file = new File(url);
						if (_file.exists()) {
							atts.put(file.getName() + file.getType(), _file);
						} else {
							log.info("邮件发送中，附件地址[" + url + "]找不到");
							;
						}
					}
				}
			}
		}
		mailInfo.setAtts(atts);
		SendEmail.send(SendEmail.HTML, mailInfo);
	}
}
